import Vue from 'vue'
import VueRouter from 'vue-router'

const HomeAuction = () => import('components/home/HomeAuction')
const HomeCreateAuction = () => import('components/home/HomeCreateAuction')
const HomeEditAuction = () => import('components/home/HomeEditAuction')
const HomeItemAuction = () => import('components/home/HomeItemAuction')
const HomeItemIndexAuction = () => import('components/home/HomeItemIndexAuction')
const HomeItemEdit = () => import('components/home/HomeItemEdit')
const HomeItemDetail = () => import('components/home/HomeItemDetail')

const Login = () => import('components/login/Login')
const LoginChat = () => import('components/login/LoginChat')

const AuctionCoupon = () => import('components/coupon/AuctionCoupon')
Vue.use(VueRouter)


const routes = [
  {
    path: "",
    redirect: '/home'
  },
  {
    path: "/home",
    component: HomeAuction,
    meta: {
      title: '123'
    }
  },
  {
    path: "/home/create",
    component: HomeCreateAuction
  },
  {
    path: "/login",
    component: Login
  },
  {
    path: "/auction/edit",
    component: HomeEditAuction
  },
  {
    path: "/auction/item/:aid",
    component: HomeItemAuction
  },
  {
    path: "/auction/index/:iid",
    component: HomeItemIndexAuction
  },
  {
    path: "/auction/item/edit/:iid",
    component: HomeItemEdit
  },
  {
    path: "/auction/index/:iid/:gid",
    component: HomeItemDetail
  },
  {
    path: "/coupon/:aid",
    component: AuctionCoupon
  },
  {
    path: "/chat",
    component: LoginChat
  }
]

const router = new VueRouter({
  routes,
  mode: 'history'
})

let White_list = ['/home', '/login']


router.beforeEach((to, from, next) => {
  let userInfo = JSON.parse(sessionStorage.getItem('userInfo'))
  let url = White_list.find(value => value === to.path)
  console.log(url);
  if (!url && !userInfo) {
    next({
      path: '/login'
    })
  }
  next()
})

export default router
